package com.wangjiang.mario.admin.web.admin.controller.v1;

import com.wangjiang.mario.admin.common.base.BaseController;
import com.wangjiang.mario.admin.common.object.UUidList;
import com.wangjiang.mario.admin.common.object.UUidListForBatchUpdate;
import com.wangjiang.mario.admin.common.rest.ResultGenerator;
import com.wangjiang.mario.admin.common.rest.entity.Result;
import com.wangjiang.mario.admin.system.domain.entity.RoleEntity;
import com.wangjiang.mario.admin.system.domain.service.RoleService;
import com.wangjiang.mario.admin.system.object.role.RoleMenusDto;
import com.wangjiang.mario.admin.system.object.role.RoleQo;
import com.wangjiang.mario.admin.system.object.role.RoleVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 *
 * @author wangjiang
 * @Date 2019-08-09 14:35:19
 */
@RestController
@RequestMapping("/role")
@Api(value = "/role", tags = "角色管理")
public class RoleController extends BaseController<RoleEntity> {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    /**
     * 为角色保存菜单及权限
     *
     * @param roleMenusDto
     * @return
     */
    @PostMapping("/menus")
    public Result saveRoleMenu(@RequestBody RoleMenusDto roleMenusDto) {
        Integer ret = roleService.updateRoleMenu(roleMenusDto.getRoleCode(), roleMenusDto.getMenuCodeList());
        return ResultGenerator.genSuccessResult(ret);
    }

    /**
     * 获取角色的菜单列表
     */
    @GetMapping("/menus")
    public Result getRoleMenu(@RequestParam String roleCode) {
        List<String> menuCodes = roleService.getMenusByRoleCode(roleCode);
        return ResultGenerator.genSuccessResult(menuCodes);
    }

    /**
     * 列表查询
     * @param qo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("")
    public Result list (
            RoleQo qo,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return ResultGenerator.genSuccessResult(roleService.basePageWithDept(currentPage, pageSize, qo, RoleVo.class));
    }

    /**
     * 根据uuid 获取实体信息
     *
     * @param uuid
     * @return
     */
    @GetMapping("/{uuid}")
    public Result getOne (@PathVariable String uuid) {
        return ResultGenerator.genSuccessResult(baseService.baseGetByUUid(uuid, RoleVo.class));
    }


    /**
     * 所有查询
     * @param qo
     * @return
     * @throws IllegalAccessException
     */
    @GetMapping("/all")
    public Result all (RoleEntity qo) throws IllegalAccessException {
        return ResultGenerator.genSuccessResult(baseService.list(qo));
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    @PostMapping("")
    public Result create (@RequestBody RoleEntity entity) {
        return ResultGenerator.genSuccessResult(roleService.saveRole(entity));
    }

    /**
     * 修改
     * @param entity
     * @return
     */
    @PutMapping("")
    public Result update(@RequestBody RoleEntity entity) {
        return ResultGenerator.genSuccessResult(roleService.saveRole(entity));
    }

    /**
     * 批量修改
     * @param uuidListForBatchUpdate
     * @return
     */
    @PutMapping("/batch")
    public Result batchUpdate(
            @RequestBody UUidListForBatchUpdate<RoleEntity> uuidListForBatchUpdate
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseUpdateBatch(uuidListForBatchUpdate));
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @DeleteMapping("")
    public Result delete(
            @RequestParam String id
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseSoftRemoveById(id));
    }

    /**
     * 批量删除
     * @param uuidList
     * @return
     */
    @DeleteMapping("/batch")
    public Result batchDelete(
            @RequestBody UUidList uuidList
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseBatchSoftRemoveByUUidList(uuidList));
    }

}
